package com.market.controller;

import com.market.entity.Order;
import com.market.entity.Store;
import com.market.service.OrderService;
import com.market.service.StoreService;
import com.market.utils.GetDistance;
import com.market.vo.R;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
public class OrderController {

    @Resource
    public OrderService orderService;
    @Resource
    public StoreService storeService;

    //新增订单
    @PostMapping("/addOrder")
    public R<Order> addOrder(@RequestBody Order order){
        boolean b = orderService.addOrder(order);
        Store store = storeService.findStore(order.getId());
        if (b){
            return new R<>(200,"订单创建成功!已自动选择最近的小店:"+store.getName(),order);
        }else {
            return new R<>(300,"订单创建失败");
        }
    }

    //通过订单编号查询一笔订单
    @GetMapping("queryOrderByOrderCode")
    public R<Order>queryOrderByOrderCode(String orderCode){
        Order order=orderService.queryOrderByOrderCode(orderCode);
        if (order!=null){
            return new R<>(200,"查询订单成功",order);
        }else {
            return new R<>(300,"查无订单!");
        }
    }


    //查询某个买家的所有订单
    @GetMapping("/queryAllBuyerOrders")
    public R<List<Order>>queryAllBuyerOrders(int buyerId){
        List<Order> orders = orderService.queryAllBuyerOrders(buyerId);
        if (orders!=null){
            return new R<>(200,"查询订单成功!共"+orders.size()+"笔历史订单",orders);
        }else {
            return new R<>(300,"查无订单!");
        }
    }

    //查询所有订单
    @GetMapping("/queryAllOrders")
    public R<List<Order>>queryAllOrders(){
        List<Order> orders = orderService.queryAllOrders();
        if (orders!=null){
            return new R<>(200,"查询订单成功!共"+orders.size()+"笔历史订单",orders);
        }else {
            return new R<>(300,"查无订单!");
        }
    }

    //查询当日8点~18点已付款的订单
    @GetMapping("/queryDayOrders")
    public R<List<Order>>queryDayOrders(String payTime){
        List<Order> orders = orderService.queryDayOrders(payTime);
        if (orders!=null){
            return new R<>(200,"查询今日已付款订单成功!共"+orders.size()+"笔订单",orders);
        }else {
            return new R<>(300,"查无订单!");
        }
    }

    //修改订单支付时间
    @GetMapping("/updateOrderPayTime")
    public R<Order>updateOrderPayTime(int id,String payTime){
        boolean b = orderService.updateOrderPayTime(id, payTime);
        if (b){
            return new R<>(200,"订单支付时间修改成功");
        }else {
            return new R<>(300,"订单支付时间修改失败");
        }
    }

    //修改订单成交时间
    @GetMapping("/updateOrderFinishTime")
    public R<Order>updateOrderFinishTime(int id,String finishTime){
        boolean b = orderService.updateOrderFinishTime(id, finishTime);
        if (b){
            return new R<>(200,"订单成交时间修改成功");
        }else {
            return new R<>(300,"订单成交时间修改失败");
        }
    }

    //修改订单状态
    @GetMapping("/updateOrderState")
    public R<Order>updateOrderState(int id,int state){
        boolean b = orderService.updateOrderState(id, state);
        if (b){
            return new R<>(200,"订单状态修改成功");
        }else {
            return new R<>(300,"订单状态修改失败");
        }
    }

    //修改订单配送地址
    @GetMapping("/updateStoreId")
     public R<Order>updateDeliveryAddress(int id,int updateStoreId){
        boolean b = orderService.updateStoreId(id, updateStoreId);
        if (b){
            return new R(200,"订单配送地址修改成功","当前配送地—"+updateStoreId);
        }else {
            return new R<>(300,"订单配送地址修改失败");
        }
    }

    //删除订单
    @GetMapping("/deleteOrder")
    public R<Order>deleteOrder(int id){
        boolean b = orderService.deleteOrder(id);
        if (b){
            return new R<>(200,"订单已删除!");
        }else {
            return new R<>(300,"订单删除失败!");
        }
    }

    //修改实付金额
    @GetMapping("/updateActualPay")
    public R<Order>updateActualPay(double money,int orderId){
        if (orderService.updateActualPay(money,orderId)){
            return new R<>(200,"修改实付金额成功");
        }else {
            return new R<>(300,"修改实付金额失败");
        }
    }

    //修改应付金额
    @GetMapping("/updateShouldPayment")
    public R<Order>updateShouldPayment(double money,int orderId){
        if (orderService.updateShouldPayment(money,orderId)){
            return new R<>(200,"修改应付金额成功");
        }else {
            return new R<>(300,"修改应付金额失败");
        }
    }
}
